import { ref, type Ref } from "vue";
import { generateId, type ITodo } from "../utils/todoStorage";

export default function useNewTodo(todosRef: Ref<ITodo[]>) {
  const newTodoRef = ref("");

  const addTodo = () => {
    const value = newTodoRef.value && newTodoRef.value.trim();
    if (!value) return;

    const todo = {
      id: generateId(),
      title: value,
      completed: false,
    };
    todosRef.value.push(todo);
    newTodoRef.value = "";
  };

  return {
    newTodoRef,
    addTodo,
  };
}
